package com.ts.api.module.match.market;

import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.IteratorUtils;
import com.gitee.apanlh.util.func.FuncFilter;
import com.ts.api.common.context.ChannelContext;
import com.ts.api.common.context.ChannelRequestContext;
import com.ts.api.common.context.ChannelUserContext;
import com.ts.api.module.channel.entity.ChannelRequest;
import com.ts.api.module.channel.entity.po.ChannelMarketPO;
import com.ts.api.module.channel.entity.po.ChannelPO;
import com.ts.api.module.match.market.internal.executor.handler.MarketExecutorHandler;
import com.ts.api.module.match.market.internal.executor.model.MarketExecuteDataExchange;
import com.ts.api.module.match.market.internal.executor.model.MarketExecuteDataExchangeResult;
import com.ts.api.module.match.market.internal.executor.model.MarketExecuteFederate;
import com.ts.api.module.match.market.internal.executor.model.MarketExecuteFederateResult;
import com.ts.api.module.match.market.internal.sort.handler.ChannelMarketSortHandler;
import com.ts.api.module.match.market.internal.sort.model.ChannelMarketSort;
import com.ts.api.module.match.market.internal.sort.model.ChannelMarketSortResult;
import com.ts.api.module.match.market.internal.valid.handler.ChannelMarketValidatorHandler;
import com.ts.api.module.match.market.internal.valid.model.ChannelMarketValidationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  渠道贷超匹配
 *
 *  @author Pan
 */
@Service
public class ChannelMarketMatchFacade {

    @Autowired
    private ChannelMarketValidatorHandler channelMarketValidatorHandler;
    @Autowired
    private MarketExecutorHandler marketExecutorHandler;
    @Autowired
    private ChannelMarketSortHandler channelMarketSortHandler;

    /**
     *  执行贷超撞库匹配
     *
     *  @author Pan
     *  @return List    返回通过业务验证且撞库联登匹配的贷超列表
     */
    public List<ChannelMarketPO> dataExchangeMatch() {
        //  返回验证通过的渠道列表
        ChannelMarketValidationResult channelMarketValidationResult = channelMarketValidatorHandler.valid();
        List<ChannelMarketPO> listMarketValidate = channelMarketValidationResult.getListMarketValidate();

        //  贷超匹配
        ChannelRequest channelRequest = ChannelRequestContext.get();
        MarketExecuteDataExchange marketExecuteDataExchange = MarketExecuteDataExchange.of(listMarketValidate, channelRequest);
        List<MarketExecuteDataExchangeResult> listExecuteResult = marketExecutorHandler.executeDataExchange(marketExecuteDataExchange);

        //  获取贷超撞库成功条目
        List<MarketExecuteDataExchangeResult> listSucResult = CollUtils.find(listExecuteResult, MarketExecuteDataExchangeResult::getHasSuc);
        List<ChannelMarketPO> listMarket = CollUtils.toList(listSucResult, t -> t.getChannelMarket());

        //  排序贷超结果
        ChannelPO channelPO = ChannelContext.get();
        ChannelMarketSort channelMarketSort = ChannelMarketSort.of(channelPO, listMarket);
        ChannelMarketSortResult channelMarketSortResult = channelMarketSortHandler.handler(channelMarketSort);
        return channelMarketSortResult.getListMarketSort();
    }

    /**
     *  执行贷超联登
     *
     *  @author Pan
     *  @param  channelMarket               渠道贷超实体
     *  @return MarketExecuteFederateResult 返回联登执行结果
     */
    public MarketExecuteFederateResult federateMatch(ChannelMarketPO channelMarket) {
        //  构建执行器
        ChannelRequest channelRequest = ChannelRequestContext.get();
        Long userId = ChannelUserContext.getId();
        MarketExecuteFederate marketExecuteFederate = MarketExecuteFederate.of(channelRequest, channelMarket, userId);

        //  联登匹配
        return marketExecutorHandler.executeFederate(marketExecuteFederate);
    }

    /**
     *  执行贷超撞库及联登流程
     *
     *  @author Pan
     *  @return List    返回通过业务验证且撞库联登匹配的贷超列表
     */
    public List<ChannelMarketPO> unionMatch() {
        return null;
    }

    /**
     *  执行贷超撞库及联登流程
     *
     *  @author Pan
     *  @return List    返回通过业务验证且撞库联登匹配的贷超列表
     */
    public List<ChannelMarketPO> unionMatch(FuncFilter<ChannelMarketPO> exclude) {
        //  返回验证通过的渠道列表
        ChannelMarketValidationResult channelMarketValidationResult = channelMarketValidatorHandler.valid();
        List<ChannelMarketPO> listMarketValidate = channelMarketValidationResult.getListMarketValidate();
        return null;
    }

    /**
     *  按照贷超排序结果依次/逐个匹配单个贷超,返回单个成功撞库的贷超
     *
     *  @author Pan
     *  @return ChannelMarketPO
     */
    public ChannelMarketPO dataExchangeMatchOneBySequence() {
        //  返回验证通过的渠道列表
        ChannelMarketValidationResult channelMarketValidationResult = channelMarketValidatorHandler.valid();
        List<ChannelMarketPO> listMarketValidate = channelMarketValidationResult.getListMarketValidate();

        //  排序贷超结果
        ChannelPO channelPO = ChannelContext.get();
        ChannelMarketSort channelMarketSort = ChannelMarketSort.of(channelPO, listMarketValidate);
        ChannelMarketSortResult channelMarketSortResult = channelMarketSortHandler.handler(channelMarketSort);
        ChannelMarketPO marketSort = IteratorUtils.getFirst(channelMarketSortResult.getListMarketSort());

        //  贷超匹配
        ChannelRequest channelRequest = ChannelRequestContext.get();
        MarketExecuteDataExchange marketExecuteDataExchange = MarketExecuteDataExchange.ofSort(marketSort, listMarketValidate, channelRequest);
        MarketExecuteDataExchangeResult executeResult = marketExecutorHandler.executeDataExchangeSequence(marketExecuteDataExchange);
        return executeResult.getChannelMarket();
    }
}
